#include "PrintUtilities.h"
#include "Utilities.h"
#include "Global.h"

#include <string>
#include <iostream>
#include <cassert>
#include <sstream>
#include <vector>
#include <fstream>

namespace Print
{
	static const size_t SCREEN_WIDTH = Consts::SCREEN_WIDTH;

	std::string RightAl(const std::string& msg)
	{
		std::string temp;
		for (size_t i = msg.length(); i < SCREEN_WIDTH; ++i)
			temp += " ";
		return temp + msg;
	}

	std::string Centred(const std::string& msg)
	{
		std::string temp;
		for (size_t i = 0; i < (SCREEN_WIDTH - msg.length()) / 2; ++i)
			temp += " ";
		return temp + msg;
	}

	std::string CenterToLeft(const std::string& msg)
	{
		std::string temp;
		for (size_t i = msg.length(); i < (SCREEN_WIDTH / 2); ++i)
			temp += " ";
		return temp + msg;		
	}

	void ClearScreen()
	{
		system("cls");
	}

	void PrintDecorationLine(std::ostream& o_stream, const char i_char)
	{
		o_stream << std::string(SCREEN_WIDTH - 1, i_char);
		//for (size_t i = 0; i < SCREEN_WIDTH - 1 ; ++i)
		//	o_stream << i_char;
	}

	void ResetInput()
	{
		if(!std::cin)
		{
			std::cin.clear();
			std::cin.ignore(1000, '\n');
			std::cin.ignore(1000, ' ');
		}
	}

	std::string Title(std::string i_title)
	{
		std::string titleBorder  = "/\\";
		//std::string titleBorder = " ";
		//titleBorder.at(0) = char (0Xce);
		std::string header = "";
		for (size_t i = header.length(); i < SCREEN_WIDTH/titleBorder.size() - 1; ++i)
			header += titleBorder;

		std::string footer = ""; //"--TITLE END:";
		for (size_t i = footer.length(); i < SCREEN_WIDTH/titleBorder.size() - 1; ++i)
			footer += titleBorder;

		return header + "\n " + i_title + "\n" + footer + "\n\n";
	}

	std::string Options(std::string i_options)
	{
		std::string optionBorder = "=";
		//optionBorder.at(0) = char (0Xcd);
		std::string header = optionBorder + optionBorder + "OPTIONS:";
		for (size_t i = header.length(); i < SCREEN_WIDTH/optionBorder.size() - 1; ++i)
			header += optionBorder;

		std::string footer = ""; //"--OPTIONS END:";
		for (size_t i = footer.length(); i < SCREEN_WIDTH/optionBorder.size() - 1; ++i)
			footer += optionBorder;

		return header + "\n" + i_options + "\n" + footer + "\n";
	}

	std::string SeparatorLine()
	{
		std::string separatorBorder = "-";
		std::string temp;
		for (size_t i = 0; i < SCREEN_WIDTH/separatorBorder.size() - 1; ++i)
			temp += separatorBorder;

		return temp + "\n";
	}

	std::string TabUpTo(const int i_val, const unsigned int i_tabLenght)
	{
		return TabUpTo(Utilities::IntToString(i_val), i_tabLenght);
	}

	std::string TabUpTo(const std::string& i_string, const unsigned int i_tabLenght)
	{
		if(i_tabLenght < i_string.size())
			return i_string.substr(0, i_tabLenght);
		std::string ret(i_string);
		ret.resize(i_tabLenght,' ');
		return ret;
	}

	void CamelCaser(const std::string& i_inFile, const std::string& i_outFile)
	{
		std::vector<std::string> names;
		std::ifstream infile(i_inFile.c_str());
		std::string s;
		const char delta = 'A' - 'a';
		while (getline(infile, s))
		{
			bool beginning = true;
			for(size_t i=0; i<s.size(); ++i)
			{
				if(!beginning)
				{
					if(s[i] == ' ')
					{
						beginning = true;
					}else{
						s[i] -=delta;
					}
				}else{
					beginning = false;
				}
			}
    		names.push_back(s);
		}

		std::ofstream ofile(i_outFile.c_str());
		for(size_t j=0; j<names.size(); ++j)
			ofile << names[j] << std::endl;
	}
}